/**
 * Project Name:shiro-progress
 * File Name:PasswordHelper.java
 * Package Name:com.pango.shiroprogress.utils
 * Date:May 15, 20149:38:52 AM
 * Copyright (c) 2014, fuqiongsong@gmail.com All Rights Reserved.
 *
*/

package com.pango.core.security.shiro.utils;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import com.pango.system.model.User;

/**
 * ClassName:PasswordHelper <br/>
 * Date:     May 15, 2014 9:38:52 AM <br/>
 * @author   thierry.fu
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
public class PasswordHelper {
	
	private final RandomNumberGenerator randomNumberGenerator =
			new SecureRandomNumberGenerator();
	
	private final String algorithm = "md5";
	
	private final int hashIterations = 2;


	public void encryptPassword(User user) {
		user.setSalt(randomNumberGenerator.nextBytes().toHex());
		
		String newpwd = new SimpleHash(algorithm, 
				user.getPassword(), 
				ByteSource.Util.bytes(user.getUsername()+user.getSalt()),
				hashIterations).toHex();
		user.setPassword(newpwd);
	}
}
